package com.gao.test;

import java.io.*;
import java.net.Socket;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
public class Client {
    /*
      TCP上传文件
     */
    public static void main(String[] args) throws IOException {
        //创建Socket对象
        Socket socket = new Socket("127.0.0.1", 8899);
        //创建网络字节输入流和输出流，用来接收和发送数据
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        //包装成缓冲字符流，方便调用readLine方法读取一行字符串
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
        //写出文件名字
        File file = new File("D://小马//A.jpg");
        writer.write(file.getName());
        writer.newLine();
        writer.flush();

        String msg = reader.readLine();
        if("ok".equals(msg)){
            FileInputStream inputStream1 = new FileInputStream(file);
            //上传文件的字节
            int len;
            byte[] bytes = new byte[1024];
            while((len=inputStream1.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
            inputStream1.close();
            //重点：给循环一个结束的标记
            socket.shutdownOutput();
         }
       String s = reader.readLine();
        System.out.println(s);
        socket.close();


    }
}
